"""FastAPI entrypoint."""
from contextlib import asynccontextmanager
from fastapi import FastAPI
from starlette.middleware.sessions import SessionMiddleware
import os
from fastapi.staticfiles import StaticFiles

from .database import engine
from .models import Base
from .routers import activate, config


@asynccontextmanager
async def lifespan(app: FastAPI):
    # On startup, create database tables
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)
    yield


app = FastAPI(title="Software Licensing Server", lifespan=lifespan)
app.add_middleware(SessionMiddleware, secret_key=os.getenv("SECRET_PEPPER", "change-me"))

app.include_router(activate.router)
app.include_router(config.router)

# admin router
from .routers import admin
app.include_router(admin.router)

# Mount downloads static directory
app.mount("/download", StaticFiles(directory="downloads"), name="downloads")


@app.get("/")
async def root():
    return {"message": "Auth server running"}
